<?php
/*
# $Id$
*/

/*
# TODO: Perhaps make the use of the Tracker optional. Check if tracking is enabled in this class only.
*/
class ModelTracker extends Object {

	/*
	# @property array $tracker
	# Stores all Model instances created via Model::create().
	# Format:
	#	$modelTracker[model-name] = array of instances
	*/
	static public $tracker = array();

	/*
	# @method void addToModelTracker( Model $model )
	# $model	= Instance to be added
	#
	# Adds $model to the global Model tracker.
	*/
	static public function addModel($model) {

		// Create entry for Model type in $tracker
		if(!isset(self::$tracker[$model->modelName])) {
			self::$tracker[$model->modelName] = array();
		}

		// Add non-persistent Model
		// TODO: Is this needed? When a Model is saved (ModelManager::save()), it is added to the ModelTracker, so will
		// be indexed by it's PK anyway. self::getModelByHash() is also not being used, and the system seems to be holding
		// together just fine! Suck it and see.
		if(!$model->isInDatabase()) {
			self::$tracker[$model->modelName][spl_object_hash($model)] = $model;
		}

		// Add persistent Model
		else {
			self::$tracker[$model->modelName][$model->getPrimaryKeyValueToken()] = $model;
		}
	}

	/*
	# @method void removeModel( Model $model )
	# $model	= Model instance
	#
	# Remove $model from the tracker.
	*/
	static public function removeModel($model) {
	}

	/*
	# @method Model|FALSE getModelByPrimaryKey( string $modelName, string $pkValue )
	# $modelName	= Model name
	# $pkValue		= Primary Key value
	#
	# Returns a Model that matches the given primary-key.
	# If the Model uses a composite primary key, then be sure to send the
	# primary key value as a string token, ie. use
	# Model::getPrimaryKeyValueToken()
	*/
	static public function getModelByPrimaryKey($modelName, $pkValue) {

		// Result
		return isset(self::$tracker[$modelName][$pkValue]) ? self::$tracker[$modelName][$pkValue] : FALSE;
	}

	/*
	# @method Model|FALSE getModelById( string $modelName, int $id )
	# $modelName	= Model name
	# $id			= Model ID
	#
	# Returns a Model that matches the given ID.
	*/
	static public function getModelById($modelName, $id) {

		// Result
		return isset(self::$tracker[$modelName][$id]) ? self::$tracker[$modelName][$id] : FALSE;
	}

	/*
	# @method Model|FALSE getModelByHash( string $modelName, string $hash )
	# $modelName	= Model name
	# $hash			= Model hash
	#
	# Returns a Model that matches the given hash.
	*/
	static public function getModelByHash($modelName, $hash) {
	}

	/*
	# @method void purge()
	#
	# Empties the tracker.
	*/
	static public function purge($modelName=NULL) {

		// TODO:
		// Make use of the "debug_zval_dump()" function to determine if a Model is actually still being referenced
		// elsewhere in the system. The returned value of this function is something like:
		//	string(11) "Hello World" refcount(3)
		//
		// See documentation because ref counts are not obvious.

		// Result
		$modelName===NULL ? self::$tracker = array() : (isset(self::$tracker[$modelName]) ? self::$tracker[$modelName] = array() : NULL);
	}

	/*
	# @method void dump()
	#
	# Debugging utility.
	*/
	static public function dump() {

		// Dump tracker contents
		foreach(self::$tracker as $mName=>$list) {
			print "<b>$mName</b><br>";
			foreach($list as $k=>$m) {
				print "$k<Br/>";
				print var_export($m->getDbData(), TRUE)."<br>";
			}
		}
	}
}
?>